package cl.mt.mvc.action;

import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cl.mt.helper.ParametroGeneraelHelper;
import cl.mt.proxy.ProxyInfoCargaVehiculo;


public class InicioAction extends Action {

	
	private Logger log = LogManager.getLogger(InicioAction.class.getName()); 
	
	  public ActionForward execute (ActionMapping mapping, ActionForm form,
              HttpServletRequest request, HttpServletResponse response) {
			
		  ParametroGeneraelHelper paramHelper = new ParametroGeneraelHelper();
		  
		  Map  map = ProxyInfoCargaVehiculo.getInstance().getMapDatos();
		  
		  servlet.getServletContext().setAttribute("totalOfertas", (String) map.get("totalOfertas"));
		  servlet.getServletContext().setAttribute("totalVehiculos", (String) map.get("totalVehiculos"));
		  servlet.getServletContext().setAttribute("resumenOfertas", (Collection) map.get("resumenOfertas"));
		  servlet.getServletContext().setAttribute("resumenVehiculos", (Collection) map.get("resumenVehiculos"));
		  

		  
		  
		  
		  getContador(request, paramHelper);
		  
		  log.debug("InicioAction..........");
		  return mapping.findForward("SUCCESS");
		  
		  
	  }

	private void getContador(HttpServletRequest request,ParametroGeneraelHelper paramHelper) {
		// TODO Auto-generated method stub
		
		if(null==request.getSession().getAttribute("LoginOK"))
		{
			log.debug("Agrego visita al contador de la tabla parametros");
			 request.setAttribute("contador", paramHelper.addVisitaContador());
		}else{
			log.debug("Busco visitas en la tabla parametros");
			 request.setAttribute("contador", paramHelper.getContadorVisita().getValor());
		}
		
	
	}
	  
	  
	 
	  
}
